package main

import (
	"context"
	"fmt"
	"time"

	"github.com/coreos/etcd/clientv3"
	"github.com/coreos/etcd/mvcc/mvccpb"
)

func main() {
	var (
		path   string
		conf   clientv3.Config
		client *clientv3.Client
		err    error
		// kv      clientv3.KV
		// getRsp  *clientv3.GetResponse
		watcher clientv3.Watcher
		watcha  clientv3.WatchChan
		event   *clientv3.Event
		// leaseRep  <-chan *clientv3.LeaseKeepAliveResponse
	)
	path = "/usr/lists"
	//set conf
	conf = clientv3.Config{
		Endpoints:   []string{"127.0.0.1:2379"},
		DialTimeout: 5 * time.Second,
	}

	//new client
	if client, err = clientv3.New(conf); err != nil {
		fmt.Println(err)
		return
	}

	// kv = clientv3.NewKV(client)
	// if getRsp, err = kv.Get(context.TODO(), path, clientv3.WithPrefix()); err != nil {
	// 	return
	// }

	watcher = clientv3.NewWatcher(client)
	watcha = watcher.Watch(context.TODO(), path, clientv3.WithPrefix())
	for watRsp := range watcha {
		fmt.Printf("%d rep\n", watRsp.Header.Revision)
		for _, event = range watRsp.Events {
			switch event.Type {
			case mvccpb.PUT:
				fmt.Printf("%s value %s PUT\n", string(event.Kv.Key), string(event.Kv.Value))
			case mvccpb.DELETE:
				fmt.Printf("%s value %s DELETE\n", string(event.Kv.Key), string(event.Kv.Value))
			}
		}
	}
}
